package de.tto.dating.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

public class Serve extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8055176089952340743L;

//	private BlobstoreService blobstoreService = BlobstoreServiceFactory
//			.getBlobstoreService();
	ImagesService imagesService = ImagesServiceFactory.getImagesService();

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
		Image img = ImagesServiceFactory.makeImageFromBlob(blobKey);

		Transform transform = ImagesServiceFactory.makeCrop(0.5, 0.5, 0.8, 0.8);
		Image newImage = imagesService.applyTransform(transform, img);

		byte[] newImageData = newImage.getImageData();
		// blobstoreService.serve(blobKey, res);

		res.setContentType("image/jpeg"); // Or image/gif or whatever, look up
											// the HTTP mime type
		OutputStream out = res.getOutputStream();

		out.write(newImageData);
		out.flush();
		out.close();

	}
}
